import org.junit.Test;

import static org.junit.Assert.*;

public class CompNumberTest {
	private static final double delta = 1E-5;
	@Test
	public void testCreate() {
		CompNumber cn1 = new CompNumber(1, 2);
		assertEquals(1, cn1.getReal(), delta);
		assertEquals(2, cn1.getImg(), delta);
		
	}
	
	@Test
	public void testPlus() {
		CompNumber cn1 = new CompNumber(1, 2);
		CompNumber cn2 = new CompNumber(3, 4);
		CompNumber cn3 = cn1.plus(cn2);
		assertEquals(4, cn3.getReal(), delta);
		assertEquals(6, cn3.getImg(), delta);
	}

	@Test
	public void testMinus() {
		CompNumber cn1 = new CompNumber(1, 2);
		CompNumber cn2 = new CompNumber(5, 4);
		CompNumber cn3 = cn1.minus(cn2);
		assertEquals(4, cn3.getReal(), delta);
		assertEquals(2, cn3.getImg(), delta);
	}

	@Test
	public void testMultiply() {
		CompNumber cn1 = new CompNumber(1, 2);
		CompNumber cn2 = new CompNumber(3, 4);
		CompNumber cn3 = cn1.multiply(cn2);
		assertEquals(-5, cn3.getReal(), delta);
		assertEquals(10, cn3.getImg(), delta);
	}

	@Test
	public void testDivision() {
		CompNumber cn1 = new CompNumber(1, 2);
		CompNumber cn2 = new CompNumber(3, 4);
		CompNumber cn3 = cn1.division(cn2);
		assertEquals(0.44, cn3.getReal(), delta);
		assertEquals(0.08, cn3.getImg(), delta);
	}

	@Test
	public void testSqrtFromPositiveReal() {
		CompNumber cn4 = new CompNumber(4, 0);
		CompNumber[] m = cn4.root(2);
		assertEquals(2, m.length);
		assertEquals(2, m[0].getReal(), delta);
		assertEquals(0, m[0].getImg(), delta);
		assertEquals(-2, m[1].getReal(), delta);
		assertEquals(0, m[1].getImg(), delta);

	}

	@Test
	public void testToString() {
		CompNumber cn1 = new CompNumber(1, 2);
		assertEquals("1.0 + 2.0i", cn1.toString());
	}

	@Test
	public void testEquals() {
		CompNumber cn1 = new CompNumber(1, 2);
		CompNumber cn2 = new CompNumber(1, 2);
		int c1 = cn1.hashCode();
		int c2 = cn2.hashCode();
		assertEquals(cn1, cn2);
		assertTrue(c1 == c2);
	}
}
